/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import DAO.DAODomicilio;
import DAO.DAOEmpresa;
import DAO.DAOFactory;
import DAO.DAOMedico;
import DAO.DAOObraSocial;
import DAO.DAOPaciente;
import DAO.DAOPuestoTrabajo;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.LinkedList;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import modelo.Domicilio;
import modelo.Empresa;
import modelo.Medico;
import modelo.ObraSocial;
import modelo.Paciente;
import modelo.PuestoTrabajo;

/**
 *
 * @author JGS
 */
public class Alta_Paciente extends javax.swing.JInternalFrame {

    private SingletonDomicilios domicilios;
    private int id_paciente;
    private Paciente pacienteSel;
    private static Alta_Paciente instancia = null;

    public static Alta_Paciente getInstancia() {
        if (instancia == null) {
            instancia = new Alta_Paciente();
        } else {
            instancia.dispose();
            instancia = new Alta_Paciente();
        }
        return instancia;
    }

    public static Alta_Paciente getInstancia(int id_paciente) {
        if (instancia == null) {
            instancia = new Alta_Paciente(id_paciente);
        } else {
            instancia.dispose();
            instancia = new Alta_Paciente(id_paciente);
        }
        return instancia;
    }

    /**
     * Creates new form Alta_Paciente
     */
    private Alta_Paciente(int id_paciente) {
        super("Modificacion Paciente",
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        DAOPaciente daoPac = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
        DAODomicilio daoDom = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getDomicilioDAO();
        this.id_paciente = id_paciente;
        pacienteSel = daoPac.find(new Paciente(id_paciente));
        initComponents();
        groupButton();
        domicilios = SingletonDomicilios.getInstancia();
        domicilios.setDomicilios((LinkedList) daoDom.list(id_paciente));
        domicilios.setPantalla(this);
        this.cargarDomicilios();
        this.cargarMedicos();
        this.cargarObrasSociales();
        cargarTiposDocumento();
        cargarPuestosTrabajo();
        cargarEmpresas();

        //carga de datos...
        id.setText(pacienteSel.getIdPaciente() + "");
        empresa.setSelectedItem(pacienteSel.getEmpresa());
        medico.setSelectedItem(pacienteSel.getMedico());
        puestoTrabajo.setSelectedItem(pacienteSel.getPuestoTrabajo());
        tipoDocumento.setSelectedItem(pacienteSel.getTipoDoc());
        obraSocial.setSelectedItem(pacienteSel.getObraSocial());
        fechaAlta.setDate(pacienteSel.getFechaAlta());
        fechaBaja.setDate(pacienteSel.getFechaBaja());
        fechaNacimiento.setDate(pacienteSel.getFechaNacimiento());
        legajo.setText(pacienteSel.getNumeroLegajo());
        nombre.setText(pacienteSel.getNombre());
        apellido.setText(pacienteSel.getApellido());
        documento.setText(pacienteSel.getNumeroDoc() + "");
        cargaFamiliar.setSelected(pacienteSel.getCargaFamiliar().equals("SI"));
        telefono.setText(pacienteSel.getTelefono() + "");
        celular.setText(pacienteSel.getCelular() + "");
        email.setText(pacienteSel.getEmail());
        femeninoRadio.setSelected(pacienteSel.getSexo().equals("F"));
        masculinoRadio.setSelected(pacienteSel.getSexo().equals("M"));
        solteroRadio.setSelected(pacienteSel.getEstadoCivil().equals("SOLTERO"));
        viudoRadio.setSelected(pacienteSel.getEstadoCivil().equals("VIUDO"));
        casadoRadio.setSelected(pacienteSel.getEstadoCivil().equals("CASADO"));
        divorciadoRadio.setSelected(pacienteSel.getEstadoCivil().equals("DIVORCIADO"));

        fechaBaja.setEnabled(true);
    }

    private Alta_Paciente() {
        super("Alta Paciente",
                false, //resizable
                true, //closable
                false, //maximizable
                true);//iconifiable
        initComponents();
        groupButton();
        domicilios = SingletonDomicilios.getInstancia();
        domicilios.setPantalla(this);
        this.cargarDomicilios();
        this.cargarMedicos();
        this.cargarObrasSociales();
        cargarTiposDocumento();
        cargarPuestosTrabajo();
        cargarEmpresas();
        fechaBaja.setEnabled(false);
    }

    private void cargarPuestosTrabajo() {
        DAOPuestoTrabajo dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPuestoTrabajoDAO();
        LinkedList<PuestoTrabajo> lista = (LinkedList) dao.list();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < lista.size(); i++) {
            PuestoTrabajo aux = lista.get(i);
            model.addElement(aux);
        }
        puestoTrabajo.setModel(model);
    }

    private void cargarEmpresas() {
        DAOEmpresa dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getEmpresaDAO();
        LinkedList<Empresa> lista = (LinkedList) dao.list();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < lista.size(); i++) {
            Empresa aux = lista.get(i);
            model.addElement(aux);
        }
        empresa.setModel(model);
    }

    private void cargarTiposDocumento() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("DNI");
        model.addElement("PASA");
        model.addElement("C.I.");
        tipoDocumento.setModel(model);
    }

    private void cargarMedicos() {
        DAOMedico dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getMedicoDAO();
        LinkedList<Medico> lista = (LinkedList) dao.list();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < lista.size(); i++) {
            Medico aux = lista.get(i);
            model.addElement(aux);
        }
        medico.setModel(model);
    }

    private void cargarObrasSociales() {
        DAOObraSocial dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getObraSocialDAO();
        LinkedList<ObraSocial> lista = (LinkedList) dao.list();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < lista.size(); i++) {
            ObraSocial aux = lista.get(i);
            model.addElement(aux);
        }
        this.obraSocial.setModel(model);
    }

    public void refrescarPantalla() {
        cargarDomicilios();
        this.setVisible(true);

    }

    private void cargarDomicilios() {
        ModeloTabla modelo = new ModeloTabla();

        Object tit[] = {"CALLE", "NUMERO", "PISO", "DEPTO", "REFERENCIA", "BARRIO", "LOCALIDAD", "PROVINCIA"};
        Object vals[][] = new Object[domicilios.getDomicilios().size()][tit.length];
        for (int i = 0; i < domicilios.getDomicilios().size(); i++) {
            Domicilio aux = domicilios.getDomicilios().get(i);
            Object datos[] = new Object[tit.length];
            datos[0] = aux.getCalle();
            datos[1] = aux.getNumero();
            datos[2] = aux.getPiso();
            datos[3] = aux.getDepto();
            datos[4] = aux.getReferencia();
            datos[5] = aux.getBarrio();
            datos[6] = aux.getLocalidad();
            datos[7] = aux.getProvincia();
            vals[i] = datos;
        }
        modelo.setDataVector(vals, tit);
        this.tablaDomicilios.setModel(modelo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        empresa = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        legajo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        nombre = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tipoDocumento = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        documento = new javax.swing.JFormattedTextField();
        jLabel9 = new javax.swing.JLabel();
        telefono = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        celular = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        femeninoRadio = new javax.swing.JRadioButton();
        masculinoRadio = new javax.swing.JRadioButton();
        jLabel14 = new javax.swing.JLabel();
        solteroRadio = new javax.swing.JRadioButton();
        casadoRadio = new javax.swing.JRadioButton();
        viudoRadio = new javax.swing.JRadioButton();
        divorciadoRadio = new javax.swing.JRadioButton();
        jLabel15 = new javax.swing.JLabel();
        nuevaObraSocial = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        agregar = new javax.swing.JButton();
        modificar = new javax.swing.JButton();
        eliminar = new javax.swing.JButton();
        guardar = new javax.swing.JButton();
        salir = new javax.swing.JButton();
        cargaFamiliar = new javax.swing.JCheckBox();
        jLabel17 = new javax.swing.JLabel();
        medico = new javax.swing.JComboBox();
        jLabel18 = new javax.swing.JLabel();
        apellido = new javax.swing.JTextField();
        fechaAlta = new com.toedter.calendar.JDateChooser();
        jLabel19 = new javax.swing.JLabel();
        fechaBaja = new com.toedter.calendar.JDateChooser();
        fechaNacimiento = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaDomicilios = new javax.swing.JTable();
        obraSocial = new javax.swing.JComboBox();
        jLabel20 = new javax.swing.JLabel();
        puestoTrabajo = new javax.swing.JComboBox();
        nuevaObraSocial1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel6.setForeground(new java.awt.Color(153, 153, 153));
        jLabel6.setText("Alta de Paciente");

        jLabel1.setText("Id:");

        id.setEditable(false);
        id.setEnabled(false);
        id.setName("id"); // NOI18N

        jLabel2.setText("Fecha Alta:");

        jLabel3.setText("Empresa:");

        empresa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("N° Legajo:");

        jLabel5.setText("Nombre:");

        jLabel7.setText("Tipo Documento:");

        tipoDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel8.setText("N°:");

        jLabel9.setText("Telefono:");

        jLabel10.setText("Celular:");

        jLabel11.setText("E-Mail:");

        jLabel12.setText("Fecha de Nacimiento:");

        jLabel13.setText("Sexo:");

        femeninoRadio.setText("Femenino");

        masculinoRadio.setText("Masculino");

        jLabel14.setText("Estado Civil:");

        solteroRadio.setText("Soltero");

        casadoRadio.setText("Casado");

        viudoRadio.setText("Viudo");

        divorciadoRadio.setText("Divorciado");

        jLabel15.setText("Obra Social:");

        nuevaObraSocial.setText("+");
        nuevaObraSocial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevaObraSocialActionPerformed(evt);
            }
        });

        jLabel16.setText("Domicilio Particular");

        agregar.setText("Agregar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarActionPerformed(evt);
            }
        });

        modificar.setText("Modificar");
        modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarActionPerformed(evt);
            }
        });

        eliminar.setText("Eliminar");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarActionPerformed(evt);
            }
        });

        guardar.setText("Grabar");
        guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarActionPerformed(evt);
            }
        });

        salir.setText("Salir");
        salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salirActionPerformed(evt);
            }
        });

        cargaFamiliar.setText("Carga Familiar");

        jLabel17.setText("Medico:");

        medico.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel18.setText("Apellido: ");

        fechaAlta.setDateFormatString("dd/MM/yyyy");

        jLabel19.setText("Fecha Baja:");

        fechaBaja.setDateFormatString("dd/MM/yyyy");

        fechaNacimiento.setDateFormatString("dd/MM/yyyy");

        tablaDomicilios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablaDomicilios.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tablaDomicilios);

        obraSocial.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel20.setText("Puesto trabajo:");

        puestoTrabajo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        nuevaObraSocial1.setText("+");
        nuevaObraSocial1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevaObraSocial1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 825, Short.MAX_VALUE)
                    .addComponent(jLabel16)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, 0)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(guardar)
                                .addGap(18, 18, 18)
                                .addComponent(salir))
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel13))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(16, 16, 16)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(femeninoRadio)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(masculinoRadio))
                                                    .addComponent(obraSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel14)
                                                    .addComponent(nuevaObraSocial))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(solteroRadio)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(casadoRadio)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(viudoRadio)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                        .addComponent(divorciadoRadio))
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addComponent(jLabel17)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(medico, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(jLabel18)
                                                            .addComponent(jLabel4))
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                                    .addGroup(layout.createSequentialGroup()
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                                .addComponent(tipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(jLabel8)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(documento, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
                                                            .addComponent(telefono, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                                                            .addComponent(email, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                                                        .addGap(28, 28, 28)
                                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(jLabel10)
                                                            .addComponent(jLabel20))
                                                        .addGap(10, 10, 10)))
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(legajo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                                    .addComponent(apellido, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                                    .addComponent(celular, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                        .addComponent(puestoTrabajo, 0, 173, Short.MAX_VALUE)
                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(nuevaObraSocial1))
                                                    .addComponent(cargaFamiliar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(15, 15, 15)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(id, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(fechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(nombre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                                            .addComponent(empresa, javax.swing.GroupLayout.Alignment.LEADING, 0, 238, Short.MAX_VALUE))
                                        .addGap(35, 35, 35)
                                        .addComponent(jLabel19)
                                        .addGap(23, 23, 23)
                                        .addComponent(fechaBaja, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(94, 94, 94))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(fechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(jLabel15)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(agregar)
                                    .addComponent(modificar)
                                    .addComponent(eliminar))))
                        .addGap(133, 133, 133)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {agregar, eliminar, modificar});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {guardar, salir});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(jLabel19)
                    .addComponent(fechaAlta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fechaBaja, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(empresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(tipoDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(documento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cargaFamiliar)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(legajo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel18))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel12)
                            .addComponent(fechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(celular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20)
                            .addComponent(puestoTrabajo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(nuevaObraSocial1))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(femeninoRadio)
                    .addComponent(masculinoRadio)
                    .addComponent(jLabel14)
                    .addComponent(solteroRadio)
                    .addComponent(casadoRadio)
                    .addComponent(viudoRadio)
                    .addComponent(divorciadoRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(nuevaObraSocial)
                    .addComponent(jLabel17)
                    .addComponent(medico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(obraSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel16)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(agregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(modificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(eliminar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardar)
                    .addComponent(salir, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {agregar, eliminar, modificar});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {guardar, salir});

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nuevaObraSocial1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevaObraSocial1ActionPerformed
        String ret = null;
        while (true) {
            ret = JOptionPane.showInputDialog(this, "Nuevo puesto", "Ingrese el nombre del puesto de trabajo",
                    JOptionPane.PLAIN_MESSAGE);
            if (ret == null) {
                return;
            } else {
                if (ret.length() == 0) {
                    JOptionPane.showMessageDialog(this, "Ingrese un nombre para el puesto de trabajo");
                } else {
                    DAOPuestoTrabajo dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPuestoTrabajoDAO();
                    dao.insert(new PuestoTrabajo(ret));
                    JOptionPane.showMessageDialog(this, "Puesto de trabajo registrado");
                    break;
                }
            }
        }
        this.cargarPuestosTrabajo();
}//GEN-LAST:event_nuevaObraSocial1ActionPerformed

    private void guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarActionPerformed
        if (comprobarCampos()) {
            if (pacienteSel == null) { //ALTA PACIENTE
                Paciente toReg = new Paciente();

                toReg.setFechaAlta(new java.sql.Date(fechaAlta.getDate().getTime()));
                toReg.setFechaBaja(null);
                toReg.setEmpresa((Empresa) empresa.getSelectedItem());
                toReg.setNumeroLegajo(legajo.getText());
                toReg.setApellido(apellido.getText());
                toReg.setNombre(nombre.getText());
                toReg.setTipoDoc((String) tipoDocumento.getSelectedItem());
                toReg.setNumeroDoc(Integer.parseInt(documento.getText()));
                if (cargaFamiliar.isSelected()) {
                    toReg.setCargaFamiliar("SI");
                } else {
                    toReg.setCargaFamiliar("NO");
                }
                if (telefono.getText().length() > 0) {
                    toReg.setTelefono(Integer.parseInt(telefono.getText()));
                } else {
                    toReg.setTelefono(0);
                }
                if (celular.getText().length() > 0) {
                    toReg.setCelular(Long.parseLong(celular.getText()));
                } else {
                    toReg.setCelular(0);
                }
                if (email.getText().length() > 0) {
                    toReg.setEmail(email.getText());
                } else {
                    toReg.setEmail(null);
                }
                toReg.setPuestoTrabajo((PuestoTrabajo) puestoTrabajo.getSelectedItem());
                toReg.setFechaNacimiento(new java.sql.Date(fechaNacimiento.getDate().getTime()));
                String sexo = "M";
                if (femeninoRadio.isSelected()) {
                    sexo = "F";
                }
                toReg.setSexo(sexo);
                String estadoCivil = "SOLTERO";
                if (casadoRadio.isSelected()) {
                    estadoCivil = "CASADO";
                }
                if (viudoRadio.isSelected()) {
                    estadoCivil = "VIUDO";
                }
                if (divorciadoRadio.isSelected()) {
                    estadoCivil = "DIVORCIADO";
                }
                toReg.setEstadoCivil(estadoCivil);
                toReg.setObraSocial((ObraSocial) obraSocial.getSelectedItem());
                toReg.setMedico((Medico) medico.getSelectedItem());

                DAOPaciente dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
                DAODomicilio daoDom = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getDomicilioDAO();

                dao.insert(toReg);

                int idPaciente = dao.ultimoReg();
                for (int i = 0; i < domicilios.getDomicilios().size(); i++) {
                    Domicilio aux = domicilios.getDomicilios().get(i);
                    aux.setUsuarioID(idPaciente);
                    daoDom.insert(aux);
                }

                JOptionPane.showMessageDialog(this, "Paciente registrado");
                this.dispose();
            } else { //MODIFICACION PACIENTE
                Paciente toReg = pacienteSel;

                toReg.setFechaAlta(new java.sql.Date(fechaAlta.getDate().getTime()));
                if (fechaBaja.getDate() != null) toReg.setFechaBaja(new java.sql.Date(fechaBaja.getDate().getTime()));
                toReg.setEmpresa((Empresa) empresa.getSelectedItem());
                toReg.setNumeroLegajo(legajo.getText());
                toReg.setApellido(apellido.getText());
                toReg.setNombre(nombre.getText());
                toReg.setTipoDoc((String) tipoDocumento.getSelectedItem());
                toReg.setNumeroDoc(Integer.parseInt(documento.getText()));
                if (cargaFamiliar.isSelected()) {
                    toReg.setCargaFamiliar("SI");
                } else {
                    toReg.setCargaFamiliar("NO");
                }
                if (telefono.getText().length() > 0) {
                    toReg.setTelefono(Integer.parseInt(telefono.getText()));
                } else {
                    toReg.setTelefono(0);
                }
                if (celular.getText().length() > 0) {
                    toReg.setCelular(Long.parseLong(celular.getText()));
                } else {
                    toReg.setCelular(0);
                }
                if (email.getText().length() > 0) {
                    toReg.setEmail(email.getText());
                } else {
                    toReg.setEmail(null);
                }
                toReg.setPuestoTrabajo((PuestoTrabajo) puestoTrabajo.getSelectedItem());
                toReg.setFechaNacimiento(new java.sql.Date(fechaNacimiento.getDate().getTime()));
                String sexo = "M";
                if (femeninoRadio.isSelected()) {
                    sexo = "F";
                }
                toReg.setSexo(sexo);
                String estadoCivil = "SOLTERO";
                if (casadoRadio.isSelected()) {
                    estadoCivil = "CASADO";
                }
                if (viudoRadio.isSelected()) {
                    estadoCivil = "VIUDO";
                }
                if (divorciadoRadio.isSelected()) {
                    estadoCivil = "DIVORCIADO";
                }
                toReg.setEstadoCivil(estadoCivil);
                toReg.setObraSocial((ObraSocial) obraSocial.getSelectedItem());
                toReg.setMedico((Medico) medico.getSelectedItem());

                DAOPaciente dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getPacienteDAO();
                DAODomicilio daoDom = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getDomicilioDAO();

                dao.update(toReg);

                int idPaciente = pacienteSel.getIdPaciente();
                daoDom.delete(toReg);
                for (int i = 0; i < domicilios.getDomicilios().size(); i++) {
                    Domicilio aux = domicilios.getDomicilios().get(i);
                    aux.setUsuarioID(idPaciente);
                    daoDom.insert(aux);
                }

                JOptionPane.showMessageDialog(this, "Paciente modificado");
                this.dispose();
            }
        }
}//GEN-LAST:event_guardarActionPerformed

    private void salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salirActionPerformed
        this.dispose();
}//GEN-LAST:event_salirActionPerformed

    private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
        int ind = tablaDomicilios.getSelectedRow();
        if (ind != -1) {
            domicilios.getDomicilios().remove(ind);
            cargarDomicilios();
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un domicilio");
        }
}//GEN-LAST:event_eliminarActionPerformed

    private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed
        // TODO add your handling code here:
        Alta_Direccion dire = new Alta_Direccion();

        this.getDesktopPane().add(dire);

        dire.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension ventana = dire.getSize();
        dire.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);
        dire.setVisible(true);
        this.setVisible(false);

    }//GEN-LAST:event_agregarActionPerformed

    private void modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarActionPerformed
        int ind = tablaDomicilios.getSelectedRow();
        if (ind != -1) {
            Alta_Direccion dire = Alta_Direccion.getInstancia(ind);
            this.getDesktopPane().add(dire);
            dire.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension ventana = dire.getSize();
            dire.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);
            dire.setVisible(true);
            this.setVisible(false);
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un domicilio");
        }
}//GEN-LAST:event_modificarActionPerformed

    private void nuevaObraSocialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevaObraSocialActionPerformed
        String ret = null;
        while (true) {
            ret = JOptionPane.showInputDialog(this, "Nueva obra social", "Ingrese el nombre de la obra social",
                    JOptionPane.PLAIN_MESSAGE);
            if (ret == null) {
                return;
            } else {
                if (ret.length() == 0) {
                    JOptionPane.showMessageDialog(this, "Ingrese un nombre para la obra social");
                } else {
                    DAOObraSocial dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getObraSocialDAO();
                    dao.insert(new ObraSocial(ret));
                    JOptionPane.showMessageDialog(this, "Obra social registrada");
                    break;
                }
            }
        }
        this.cargarObrasSociales();
}//GEN-LAST:event_nuevaObraSocialActionPerformed

    private boolean comprobarCampos() {
        if (fechaAlta.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Ingrese una fecha de alta");
            return false;
        }
        if (legajo.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese un legajo");
            return false;
        }
        if (nombre.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese un nombre");
            return false;
        }
        if (documento.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Ingrese un numero de documento");
            return false;
        }
        if (fechaNacimiento.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Ingrese una fecha de nacimiento");
            return false;
        }
        if (!masculinoRadio.isSelected() && !femeninoRadio.isSelected()) {
            JOptionPane.showMessageDialog(this, "Seleccione un sexo");
            return false;
        }
        if (!solteroRadio.isSelected() && !casadoRadio.isSelected() &&
                !viudoRadio.isSelected() && !divorciadoRadio.isSelected()) {
            JOptionPane.showMessageDialog(this, "Seleccione un estado civil");
            return false;
        }
        //checkeo de domicilios:
        SingletonDomicilios domic = SingletonDomicilios.getInstancia();
        if (domic.getDomicilios().size() == 0) {
            JOptionPane.showMessageDialog(this, "Debe ingresar al menos un domicilio");
            return false;
        }
        return true;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregar;
    private javax.swing.JTextField apellido;
    private javax.swing.JCheckBox cargaFamiliar;
    private javax.swing.JRadioButton casadoRadio;
    private javax.swing.JTextField celular;
    private javax.swing.JRadioButton divorciadoRadio;
    private javax.swing.JFormattedTextField documento;
    private javax.swing.JButton eliminar;
    private javax.swing.JTextField email;
    private javax.swing.JComboBox empresa;
    private com.toedter.calendar.JDateChooser fechaAlta;
    private com.toedter.calendar.JDateChooser fechaBaja;
    private com.toedter.calendar.JDateChooser fechaNacimiento;
    private javax.swing.JRadioButton femeninoRadio;
    private javax.swing.JButton guardar;
    private javax.swing.JTextField id;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField legajo;
    private javax.swing.JRadioButton masculinoRadio;
    private javax.swing.JComboBox medico;
    private javax.swing.JButton modificar;
    private javax.swing.JTextField nombre;
    private javax.swing.JButton nuevaObraSocial;
    private javax.swing.JButton nuevaObraSocial1;
    private javax.swing.JComboBox obraSocial;
    private javax.swing.JComboBox puestoTrabajo;
    private javax.swing.JButton salir;
    private javax.swing.JRadioButton solteroRadio;
    private javax.swing.JTable tablaDomicilios;
    private javax.swing.JTextField telefono;
    private javax.swing.JComboBox tipoDocumento;
    private javax.swing.JRadioButton viudoRadio;
    // End of variables declaration//GEN-END:variables

    private void groupButton() {

        ButtonGroup sexo = new ButtonGroup();

        sexo.add(femeninoRadio);
        sexo.add(masculinoRadio);
        ButtonGroup estadoCivil = new ButtonGroup();
        estadoCivil.add(solteroRadio);
        estadoCivil.add(casadoRadio);
        estadoCivil.add(viudoRadio);
        estadoCivil.add(divorciadoRadio);

    }
}


